﻿using System;
using MFilesAPI;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace FSAG.UserImporter
{
    /// <summary>
    /// Main programm act as the starting point contains the main method
    /// </summary>
    public class UserImporterProgram
    {
        /// <summary>
        /// Used to start the import procedure
        /// </summary>
        public static void Main(string[] args)
        {
            var overrideUser = false;
            var overrideGroup = false;

            Console.WriteLine("================");
            Console.WriteLine("User-Importer");
            Console.WriteLine("================");
            Console.WriteLine();

            var userListObjID = new ObjIDClass { ID = ConfigManager.GetSettingAsNumber("ObjIDUserListDefault"), Type = 0 };

            if (args.Length > 0)
            {
                var userListType = args[0];
                if (userListType == ConfigManager.GetSetting("ExcelParamWP"))
                {
                    userListObjID = new ObjIDClass { ID = ConfigManager.GetSettingAsNumber("ObjIDUserListWP"), Type = 0 };
                }
                else if (userListType == ConfigManager.GetSetting("ExcelParamSTEX"))
                {
                    userListObjID = new ObjIDClass { ID = ConfigManager.GetSettingAsNumber("ObjIDUserListSTEX"), Type = 0 };
                }
                else if (userListType == ConfigManager.GetSetting("ExcelParamWBIZLG"))
                {
                    userListObjID = new ObjIDClass { ID = ConfigManager.GetSettingAsNumber("ObjIDUserListWBIZLG"), Type = 0 };
                }
                else if (userListType == ConfigManager.GetSetting("ExcelParamDozenten"))
                {
                    userListObjID = new ObjIDClass { ID = ConfigManager.GetSettingAsNumber("ObjIDUserListDozenten"), Type = 0 };
                }

                overrideUser = ContainsString(args, ConfigManager.GetSetting("ExcelParamOverrideUser"));
                overrideGroup = ContainsString(args, ConfigManager.GetSetting("ExcelParamOverrideGroup"));

                var arguments = string.Empty;
                Array.ForEach(args, s => arguments += s + " ");
                ConfigManager.Log.InfoFormat("Read in Argument: {0}", arguments);
            }
            ConfigManager.Log.InfoFormat("FileID: {0}", userListObjID.ID);
            ImportManager.StartImportProcedure(userListObjID, overrideUser, overrideGroup);
        }
        
        /// <summary>
        /// Simple method for checking wheter a given string array contains a certain string value 
        /// </summary>
        /// <param name="values">Array of string values</param>
        /// <param name="value">Value to check</param>
        /// <returns></returns>
        private static bool ContainsString(string[] values, string value)
        {
            return Array.Exists(values, s => s == value);
        }
    }
}